package com.yunhe.option.repository.data;

import com.yunhe.option.domain.data.SeasonPrice;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SeasonPriceRepository extends Neo4jRepository<SeasonPrice, Long> {
    @Query("start s=Node({seasonId}), c=Node({costPriceId}) create (c)-[:HAS_SEASON_PRICE]->(s)")
    void bindCostPrice(@Param("seasonId") Long seasonId, @Param("costPriceId") Long costPriceId);

    @Query("start s=Node({id}) match (cp:CostPrice)-[]-(s) return ID(cp)")
    Long findCostPriceId(@Param("id") Long id);

    @Query("start s=Node({seasonId}) match (p:PriceRate) where p.code in {priceRateCode} create (s)-[:PRICERATE_OF]->(p)")
    void bindPriceRate(@Param("seasonId") Long seasonId, @Param("priceRateCode") List<Long> priceRateCode);

    @Query("start s=Node({id}) match (s)-[]-(d:SeasonPriceDetail) detach delete d, s")
    void deleteSeasonPrice(@Param("id") Long id);

    @Query("start c=Node({id}) match (c)-[]-(s:SeasonPrice) where s.title = {title} return s")
    SeasonPrice findByCostPriceIdAndTitle(@Param("id") Long id, @Param("title") String title);

    @Query("start c=Node({id}) match (c:CostPrice)-[:HAS_SEASON_PRICE]->(s:SeasonPrice)-[:HAS_DETAIL]->(d:SeasonPriceDetail) return s, d")
    List<SeasonPrice> findSeasonPriceByCostPrice(@Param("id") Long id);
}
